package lormi.meipin.service.impl;


import lormi.meipin.dto.MerChantEditDto;
import lormi.meipin.dto.RegMerChant;
import lormi.meipin.entity.*;
import lormi.meipin.exception.LMException;
import lormi.meipin.service.BaseService;
import lormi.meipin.service.IMerChantService;
import lormi.meipin.service.ITokenService;
import lormi.meipin.util.CacheUtil;
import lormi.meipin.util.StringUtil;
import lormi.meipin.vo.LoginVo;
import lormi.meipin.vo.MerchantCenterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class MerChantService extends BaseService implements IMerChantService{

	@Autowired
	private ITokenService tokenService;
	

	@Transactional
	@Override
	public LoginVo regMerChant(RegMerChant regMerChant) throws LMException {
		//查询该手机是否已注册
		if (memberMapper.selectByPhone(regMerChant.getPhone())!=null) {
			throw new LMException(100021);
		}
		//如果没有注册则注册该手机号
		if (memberMapper.insert(new Member(regMerChant.getPhone(),new Byte("2")))<1) {
			throw new LMException(150002);
		}
		Member member = memberMapper.selectByPhone(regMerChant.getPhone());
		//插入商家信息数据
		merChantMapper.insert(new MerChant(member.getId(),regMerChant));
		if (merChantMapper.selectByMerId(member.getId())==null) {
			throw new LMException(150008);
		}

		new Thread(new Runnable() {//无关操作使用多线程
			@Override
			public void run() {
				//插入用户自己的邀请码
				invitationCodeMapper.insert(new InvitationCode(member.getId(),StringUtil.get6Code()));
				//插入相应的日志
				loginLogMapper.insert(new LoginLog(regMerChant,member.getId()));
				locationMapper.insert(new Location(regMerChant,member.getId(),member.getUsertype()));
				locationLogMapper.insert(new LocationLog(regMerChant.getLongitude(),regMerChant.getLatitude(),member.getId()));
				//插入邀请日志
				String code = CacheUtil.getCacheUtil().getOtherCacheToStr("invite_"+regMerChant.getPhone());
				if (StringUtil.isNotNull(code)) {//存在邀请码
					InvitationCode invitationCode = invitationCodeMapper.selectByCode(Integer.valueOf(code));
					invitationLogMapper.insert(new InvitationLog(invitationCode.getCode(),invitationCode.getUserid(),member.getId(),invitationCode.getCreatetime(),member.getCreatetime()));
				}
			}
		}).start();
		//集成返回结果
		LoginVo loginVo = new LoginVo();
		loginVo.setId(member.getId());
		loginVo.setUsertype(member.getUsertype());
		loginVo.setMobile(member.getMobile());
		loginVo.setCreatetime(new Date());
		loginVo.setUpdatetime(loginVo.getCreatetime());
		loginVo.setToken(tokenService.insertToken(member.getId()).getToken());
		return loginVo;
	}


	@Override
	public void editMerChant(MerChantEditDto merChantEditDto) throws LMException {
		MerChant merChant = merChantMapper.selectByMerId(merChantEditDto.getUserid());
		
		merChant.setMerid(merChantEditDto.getUserid());
		merChant.setPhoto(merChantEditDto.getImgurl());
		merChant.setName(merChantEditDto.getUsername());
		merChant.setSex(merChantEditDto.getSex());
		merChant.setTitle(merChantEditDto.getTitle());
		merChant.setCompany(merChantEditDto.getCompany());
		merChant.setFirmsize(merChantEditDto.getFirmsize());
		merChant.setNature(merChantEditDto.getNature());
		merChant.setAddress(merChantEditDto.getAddress());
		merChant.setLabel(merChantEditDto.getLabel());
		
		if(merChantMapper.updateByPrimaryKey(merChant)<1){
			throw new LMException(130010);
		}
	}


	@Override
	public MerChant getMerChant(Integer userid) throws LMException {
		MerChant merChant = merChantMapper.selectByMerId(userid);
		if (merChant==null) {
			throw new LMException();
		}
		String sex;
		if (merChant.getSex()==1) {
			sex = "男";
		}else{
			sex = "女";
		}
		merChant.setSexname(sex);
		merChant.setLabels(labelTemplateMapper.selectByList(StringUtil.getSplit(merChant.getLabel(),";")));
		return merChant;
	}


	@Override
	public MerchantCenterVo getMerChantCenter(Integer userid) throws LMException {
		MerchantCenterVo merchantCenterVo = new MerchantCenterVo();
		MerChant merChant = merChantMapper.selectByMerId(userid);
		merchantCenterVo.setMerid(merChant.getMerid());
		merchantCenterVo.setName(merChant.getName());
		merchantCenterVo.setPhoto(merChant.getPhoto());
		merchantCenterVo.setCompany(merChant.getCompany());
		merchantCenterVo.setTitle(merChant.getTitle());
		merchantCenterVo.setEnvironmentTime(environmentMapper.selectCountByMerId(userid));
		merchantCenterVo.setFocusTimes(focusTalentMapper.selectFocusTimes(userid));
		merchantCenterVo.setPositionTime(positionMapper.selectCount(userid));
		
		return merchantCenterVo;
	}
	
	

}
